package com.lxb.store.service.Impl;

import com.lxb.store.dao.BasicPageDao;
import com.lxb.store.entity.BasicPage;
import com.lxb.store.service.BasicPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BasicPageServiceImpl implements BasicPageService{

    @Autowired
    private BasicPageDao basicPageDao;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    private String key = "basicPage";
    @Override
    public BasicPage getById(Integer id) {

        BasicPage basicPage = null;

        //判断是否存在redis缓存里
        if(redisTemplate.hasKey(key)){
            System.out.println("在redis里面查询！");
            List<Object> basicPageList = new ArrayList<>();
            basicPageList = redisTemplate.opsForList().range(key,0,1);
            basicPage = (BasicPage) basicPageList.get(0);
        }
        else{
            //数据库查询
            System.out.println("数据库里查询");
            basicPage = basicPageDao.getById(id);
            redisTemplate.opsForList().leftPush(key,basicPage);
        }

        return  basicPage;

    }


}
